package com.zy.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.mapper.PermissionMapper;
import com.zy.mapper.RolePermissionMapper;
import com.zy.pojo.Permission;
import com.zy.pojo.RolePermission;
import com.zy.vo.TreeNode;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("permissionService")
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService{
//    @Resource
//    private PermissionMapper permissionMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;
    //特有的查询节点方法
    private List<TreeNode> getnodes(Integer pid,Integer roleId){
       List<Integer> perids=rolePermissionMapper.getPerIdByRoleId(roleId);
        List<TreeNode> nodes=new ArrayList<>();
        List<Permission> list=getPermissions(pid);
        for(Permission permission:list){
            TreeNode treeNode=new TreeNode();
            treeNode.setId(permission.getId());
            treeNode.setTitle(permission.getName());
            treeNode.setExpand(false);
            //判断是不是;最小的节点
            QueryWrapper queryWrapper1=new QueryWrapper();
            queryWrapper1.eq("parent_id",permission.getId());
            this.baseMapper.selectCount(queryWrapper1);
            if(perids.contains(permission.getId())){
                treeNode.setChecked(true);
            }
            nodes.add(treeNode);
        }
        return nodes;
    }

    @Override
    public List<TreeNode> gteTreeNodes(Integer roleId) {
        //一级节点
        List<TreeNode> nodes01=this.getnodes(null,roleId);
        for(TreeNode node01:nodes01){
            //二级节点
            List<TreeNode> nodes02=this.getnodes(node01.getId(),roleId);
            node01.setChildren(nodes02);
            for(TreeNode node02:nodes02){
                List<TreeNode> nodes03=this.getnodes(node02.getId(),roleId);
                node02.setChildren(nodes03);
            }
        }
        return nodes01;
    }

    @Override
    public void dogrant(Integer roleId, Integer[] nid) {
        //根据perid删除 中间表
        rolePermissionMapper.deleteById(roleId);
       for(Integer perid:nid){
           rolePermissionMapper.insert(new RolePermission(roleId,perid));
       }
    }

    @Override
    public List<String> getPercodes(Integer roleid) {
        return baseMapper.getPercodes(roleid);
    }
    private List<Permission> getPermissions(Integer pid) {
        QueryWrapper queryWrapper=new QueryWrapper();
        if(pid==null){
            queryWrapper.isNull("parent_id");
        }else {
            queryWrapper.eq("parent_id",pid);
        }
//        orderByDesc降序 orderByAsc升序
        queryWrapper.orderByAsc("sort");
//        List<Permission> list=super.list(queryWrapper);
        return super.list(queryWrapper);
    }

    @Override
    public List<Permission> getPerMenus() {
        Subject subject= SecurityUtils.getSubject();
        //一级菜单目录
        List<Permission> menus01=this.getPermissions(null);
        menus01=menus01.stream().filter(permission -> subject.isPermitted(permission.getPermCode())).collect(Collectors.toList());
        for (Permission p01:menus01){
            //二级菜单栏目
            List<Permission> menus02=this.getPermissions(p01.getId());
            menus02=menus02.stream().filter(permission -> subject.isPermitted(permission.getPermCode())).collect(Collectors.toList());

            p01.setChildren(menus02);
        }
        return menus01;
    }
}
